package edu.wayne.cs.csc4110.hw6.framework.gui.inbox;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.plaf.basic.BasicComboBoxRenderer;

import edu.wayne.cs.csc4110.hw6.framework.gui.lobby.GameButton;
import edu.wayne.cs.csc4110.hw6.framework.lobby.GameLobbyServer;
import edu.wayne.cs.csc4110.hw6.framework.message.InboxMessage;
import edu.wayne.cs.csc4110.hw6.framework.message.MessageServer;
import edu.wayne.cs.csc4110.hw6.interfaces.user.User;

public class PrivateChatForm extends JPanel {
	private JTextField subjectField;
	private JComboBox<User> recipientComboBox;
	private JLabel recipientLabel, subjectLabel;
	private JTextArea messageArea;
	private JButton sendBtn;
	private List<User> users;
	private Vector<User> userModel;
	private JPanel recipientContainer;
	private JPanel subjectContainer;
	private JPanel top;
	private User activeUser;

	public PrivateChatForm(final User sender, final User recipient) {
		this(sender);
		recipientComboBox.setSelectedItem(recipient);
	}

	public PrivateChatForm(final User user) {
		init();
		activeUser = user;

		users = GameLobbyServer.getInstance().getUsers();
		for (User u : users) {
			userModel.add(u);
		}

		recipientContainer.add(recipientLabel);
		recipientContainer.add(recipientComboBox);
		subjectContainer.add(subjectLabel);
		subjectContainer.add(subjectField);

		top.add(recipientContainer);
		top.add(subjectContainer);
		this.add(top, BorderLayout.NORTH);
		this.add(messageArea, BorderLayout.CENTER);
		this.add(sendBtn, BorderLayout.SOUTH);

	}

	private void init() {
		userModel = new Vector<User>();
		sendBtn = new GameButton("Send");
		messageArea = new JTextArea();
		messageArea.setLineWrap(true);
		messageArea.setWrapStyleWord(true);
		recipientComboBox = new JComboBox<User>(userModel);
		recipientComboBox.setRenderer(new UserRenderer());
		recipientContainer = new JPanel();
		recipientContainer.setLayout(new FlowLayout(FlowLayout.LEFT));
		subjectContainer = new JPanel();
		subjectContainer.setLayout(new FlowLayout(FlowLayout.LEFT));
		subjectField = new JTextField(20);
		recipientLabel = new JLabel("To:");
		subjectLabel = new JLabel("Subject:");
		this.setLayout(new BorderLayout());
		top = new JPanel();
		sendBtn.addActionListener(new MessageSendListener());

	}

	class MessageSendListener implements ActionListener {

		private MessageServer server = MessageServer.getInstance();

		@Override
		public void actionPerformed(ActionEvent e) {
			if (recipientComboBox.getSelectedItem() != null) {
				if(messageArea.getText().length() < 140){
					InboxMessage msg = new InboxMessage(activeUser,
							(User) recipientComboBox.getSelectedItem(),
							messageArea.getText(), subjectField.getText());
					if (server.storeMessage(msg)) {
						JOptionPane.showMessageDialog(recipientComboBox,
								"Message was sent!");
						recipientComboBox.setSelectedIndex(-1);
						messageArea.setText("");
						subjectField.setText("");
					} else {
						JOptionPane.showMessageDialog(messageArea,
								"Message was not sent!");
					}
				}else{
					JOptionPane.showMessageDialog(messageArea, "Message must be less that 140 characters");
				}

			} else {
				JOptionPane.showMessageDialog(messageArea,
						"You must select a user to send a message to ");
			}

		}

	}

	class UserRenderer extends BasicComboBoxRenderer {
		public Component getListCellRendererComponent(JList list, Object value,
				int index, boolean isSelected, boolean cellHasFocus) {
			super.getListCellRendererComponent(list, value, index, isSelected,
					cellHasFocus);

			if (value != null) {
				User user = (User) value;
				setText(user.getUsername());
			}

			return this;
		}
	}
}
